<?php
namespace app\admin\controller;
use app\parent\admin;
use think\Db;

class Index extends admin
{
    /**
    * showdoc
    * @catalog /主后台页面/首页
    * @title 导航页面
    * @description 主后台页面
    * @url /index.php/admin/index/index
    * @method get
    * @return 
    * @remark 
    */
    public function index()
    {

        $data = [
            'path' => '/admin.php/',
            'arr'  => $this->returnNav()
        ];

        return view('index',$data);
    }

    public function returnNav($pid = 0, $arr = [])
    {
        $admin_rank_content = session('admin_rank_content');

        $info = Db::name('admin_nav')->where('admin_nav_pid',$pid)->where('admin_nav_is_show',1)->order('admin_nav_sort asc')->select();

        $arr = [];

        if($info){
            foreach($info as $k => $v){
                if(isset($admin_rank_content[$v['id']])){
                    $arr[] = [
                        'title' => $v['admin_nav_title'],
                        'url' => $v['admin_nav_url'].'?adminrankid='.$v['id'].'&adminrankkey=title',
                        'arr' => $this->returnNav($v['id'])
                    ];
                }
            }
        }

        return $arr;
    }

    /**
    * showdoc
    * @catalog /主后台页面/首页
    * @title 后台控制台
    * @description 主后台页面
    * @url /index.php/admin/index/home
    * @method get
    * @return 
    * @remark 
    */
    public function home()
    {
        return view();
    }

    // 登陆
    public function login()
    {
        if(!$this->isPost()){
            return view('login');
        }

        if (!captcha_check($this->input('code'))){
            
            return back('验证码错误!',1,'Tmsg',1);

        }

        $default = get_default();
        
        $ip = explode(',',$default['ip']);

        if(in_array($this->request->ip(),$ip)){

            return back('您不能在这里登陆!',1,'Tmsg',1);

        }

        $pass_date = session('pass_date');
        $pass_con = session('pass_con');
        $dte = date('y-m-d');
        
        if($pass_date){
            if(($default['max'] - $pass_con) < 1){

                return back('您今天的登陆次数已经没有了,请明天再试!',1,'Tmsg',1);

            }
        }

        $username = trim($this->input('username',0));
        $password = trim($this->input('password',0));
        if(!$username && !$password){

            return back('请填写账户和密码!',1,'Tmsg',1);

        }

        $user = Db::name('admin_user')->where('admin_user_name',$username)->find();
        if($user){
            if($user['admin_user_is_show']){
                if(md5(md5($password)) == $user['admin_user_pass']){
                    
                    $admin_session = [];

                    if($user['admin_user_rank_id'] != 0){
                        $rank = Db::name('admin_rank')->where('id',$user['admin_user_rank_id'])->find();
                        if(!$rank){
                            return back('您的操作权限丢失!',1,'Tmsg',1);
                        }

                        $admin_rank_content = $rank['admin_rank_content'] ? json_decode($rank['admin_rank_content'],true) : [];

                        $admin_session  +=  [
                            'admin_rank_title'    =>  $rank['admin_rank_title'],
                            'admin_rank_content'      =>  $admin_rank_content
                        ];

                    }else{
                        $nav = Db::name('admin_nav')->select();
                        $admin_rank_content = [];
                        if($nav){
                            foreach($nav as $k => $v){
                                $admin_rank_content[$v['id']]['title'] = $v['admin_nav_title'];
                                if($v['admin_nav_but']){
                                    $but = explode(',',$v['admin_nav_but']);
                                    if($but){
                                        foreach($but as $ks => $vs){
                                            $buts = explode('=',$vs);
                                            if($buts && isset($buts[1])){
                                                $admin_rank_content[$v['id']][$buts[0]] = $buts[1];
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        $admin_session  +=  [
                            'admin_rank_title'    =>  '超级管理员',
                            'admin_rank_content'      =>  $admin_rank_content
                        ];
                    }

                    $admin_session  +=  [
                        'adminid'               =>  $user['id'],
                        'admin_user_pid'        =>  $user['admin_user_pid'],
                        'admin_user_rank_id'    =>  $user['admin_user_rank_id'],
                        'admin_user_nickname'   =>  $user['admin_user_nickname'],
                        'admin_user_con'        =>  $user['admin_user_con']+1,
                        'admin_user_top_ip'     =>  $user['admin_user_top_ip'],
                        'admin_user_x_ip'       =>  $this->request->ip(),
                        'admin_user_top_time'   =>  $user['admin_user_top_time'],
                        'pass_date'             =>  0,
                        'pass_con'              =>  0,
                    ];
                    
                    $this->sessionSet($admin_session);

                    Db::name('admin_user')->where('id',$user['id'])->update(['admin_user_con'=>$user['admin_user_con']+1,'admin_user_top_ip'=>$this->request->ip(),'admin_user_top_time'=>time()]);

                    Db::name('admin_log')->insert([
                        'admin_log_admin_name'      =>  $user['admin_user_nickname'],   // 管理昵称
                        'admin_log_admin_ip'        =>  $this->request->ip(),           // 登陆ip
                        'admin_log_times'           =>  time(),                          // 时间
                        'admin_log_event'           =>  '网站后台登陆',                 // 事件
                        'admin_log_content'         =>  date('Y-m-d H:i').' '.$pass_con.'次后网站后台登陆成功',// 事件
                        'admin_log_rank_name'       =>  $admin_session['admin_rank_title']    // 权限
                    ]);

                    return redirect('/admin.php');

                }else{
                    $admin_session = [];

                    if($pass_date){
                        if($pass_date == $dte){

                            $admin_session['pass_con'] = $pass_con+1;

                        }else{

                            $admin_session['pass_date'] = $dte;
                            $admin_session['pass_con'] = 1;

                        }
                    }else{

                        $admin_session['pass_date'] = $dte;
                        $admin_session['pass_con'] = 1;

                    }

                    Db::name('admin_log')->insert([
                        'admin_log_admin_name'      =>  $user['admin_user_nickname'],   // 管理昵称
                        'admin_log_admin_ip'        =>  $this->request->ip(),           // 登陆ip
                        'admin_log_times'           =>  time(),                          // 时间
                        'admin_log_event'           =>  '网站后台登陆失败',                 // 事件
                        'admin_log_content'         =>  date('Y-m-d H:i').' 第'.$admin_session['pass_con'].'次登陆失败',                 // 事件
                        'admin_log_rank_name'       =>  '未知'    // 权限
                    ]);

                    $this->sessionSet($admin_session);

                    $dshu = $default['max'] - ($pass_con + 1);
                    return back('密码错误,今日还有 '.$dshu.' 次机会!',1,'Tmsg',1);

                }
            }else{

                return back('账户已被禁止登陆',1,'Tmsg',1);

            }
        }else{

            return back('账户不存在!',1,'Tmsg',1);

        }
    }

    // 退出
    public function loginDel()
    {
        session(null);
        return redirect('/admin.php/index');
        // return back();
    }

    public function loginInfo()
    {
        $nav = Db::name('admin_nav')->select();
        $admin_rank_content = [];
        if($nav){
            foreach($nav as $k => $v){
                $admin_rank_content[$v['id']]['title'] = $v['admin_nav_title'];
                if($v['admin_nav_but']){
                    $but = explode(',',$v['admin_nav_but']);
                    if($but){
                        foreach($but as $ks => $vs){
                            $buts = explode('=',$vs);
                            if($buts && isset($buts[1])){
                                $admin_rank_content[$v['id']][$buts[0]] = $buts[1];
                            }
                        }
                    }
                }
            }
        }
        
        $this->sessionSet('admin_rank_content',$admin_rank_content);
        return back();
    }

    
}
